package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/images")
public class ImageController {

    /**
     * 图片上传处理
     * @param file 图片上传的参数名
     * @return
     */
    @PostMapping
    public Map upload(MultipartFile file){
        Map<String,Object> map=new HashMap<>();
        try {
            //将文件保存到指定的目录
            //String url = UploadUtil.upload(file, UploadUtil.PATH+"/upload");
            //使用七牛云图片服务器
            String url = UploadUtil.uploadQiNiuYun(file);

            map.put("status",1);
            map.put("url",UploadUtil.QINIU_PATH+url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",0);
            map.put("msg","上传失败");
        }
        return map;
    }
}
